﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Windows.Forms;

namespace FotoAnotSharp.Anotator
{
    /// <summary>
    /// This class load and save only polygons without anotations
    /// </summary>
    public class AnotFile
    {
        public byte[] Background { get; set; }
        public List<FilePolygon> Polygons { get; set; }

        public int NextId { get; set; }

        /// <summary>
        /// save both image data and polygons to file
        /// </summary>
        /// <param name="file">object with image data and polygons</param>
        /// <param name="hiearchy">file path</param>
        public static void Save(AnotFile file, string fileName)
        {
            XmlSerializer ser = new XmlSerializer(typeof(AnotFile));

            try
            {
                using (FileStream str = new FileStream(fileName, FileMode.Create))
                {
                    ser.Serialize(str, file);
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("Soubor se nepovedlo uložit.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Debug.WriteLine(er.Message + er.StackTrace);
            }
        }

        /// <summary>
        /// load image data and polygons from file
        /// </summary>
        /// <param name="fileName">file path</param>        
        public static AnotFile Load(string fileName)
        {
            XmlSerializer ser = new XmlSerializer(typeof(AnotFile));
            try
            {
                using (FileStream str = new FileStream(fileName, FileMode.Open))
                {
                    AnotFile ret = (AnotFile)ser.Deserialize(str);

                    return ret;
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("Soubor se nepovedlo otevřít.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Debug.WriteLine(er.Message + er.StackTrace);
                return null;
            }
        }
    }

    /// <summary>
    /// This class contain list of points of polygon
    /// and id of polygon
    /// </summary>
    public class FilePolygon
    {
        public List<PointF> Points { get; set; }
        public int ID { get; set; }
    }
}
